﻿using DTcms.Common;
using System.Web.Mvc;

namespace DTcms.Web.Mvc.UI.Page
{
    public partial class login : BaseController
    {
        protected string turl = string.Empty;
        protected string redirect = string.Empty;//回传地址

        /// <summary>
        /// OnInit事件,检查用户是否已经登录
        /// </summary>
        public login()
        {
            turl = linkurl("usercenter", "index");
            if (System.Web.HttpContext.Current.Request.Url != null && System.Web.HttpContext.Current.Request.UrlReferrer != null)
            {
                string currUrl = System.Web.HttpContext.Current.Request.Url.ToString().ToLower(); //当前页面
                string refUrl = System.Web.HttpContext.Current.Request.UrlReferrer.ToString().ToLower(); //上一页面
                string regPath = linkurl("register").ToLower(); //注册页面
                if (currUrl != refUrl && refUrl.IndexOf(regPath) == -1 && refUrl.IndexOf("login") == -1)
                {
                    turl = System.Web.HttpContext.Current.Request.RawUrl.ToString();
                }
            }
            if (turl == "")
            {
                turl = DTRequest.GetQueryString("from");
            }
            Utils.WriteCookie(DTKeys.COOKIE_URL_REFERRER, turl); //记住上一页面

            Model.users model = GetUserInfo();
            if (model != null)
            {
                //写入登录日志
                new BLL.user_login_log().Add(model.id, model.user_name, "自动登录");

                //自动登录,跳转URL
                redirect = turl;
            }
        }
    }
}
